package com.tengju.bff.infrastructure.config;

import com.tengju.bff.interfaces.shared.servlet.ParamKeyConstant;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;


@Configuration
@EnableSwagger2
@ConditionalOnExpression("${swagger.enable}")
public class SwaggerConfig {

    @Value("${swagger.enable}")
    private boolean enableSwagger;

    @Value("${swagger.base.package}")
    private String basePackage;

    @Bean
    public Docket createRestApi() {
        ParameterBuilder tokenPar = new ParameterBuilder();
        ParameterBuilder fromPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        tokenPar.name(ParamKeyConstant.TOKEN).description("用户token").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        fromPar.name(ParamKeyConstant.FROM).description("终端from：tj").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());
        pars.add(fromPar.build());
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(enableSwagger)
                .select()
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("DT应用")
                .description("DT应用")
                .termsOfServiceUrl("http://localhost:8080/")
                .contact(new Contact("wen", "bba", "dd"))
                .version("1.0")
                .build();
    }

//    @Bean
//    public Docket allParamsPage() {
//        return new Docket(DocumentationType.SWAGGER_2)
//                .select()
//                .paths(or(regex("/mercury/.*")))
//                .build()
//                .groupName("全局配置")
//                .genericModelSubstitutes(Result.class)
//                .useDefaultResponseMessages(false)
//                .forCodeGeneration(true)
//                .pathMapping("/")// base，最终调用接口后会和paths拼接在一起
//                .apiInfo(new ApiInfo("首页", "", "", "", new Contact("北方", "", ""), "", "", Collections.emptyList()));
//    }

}
